<?php
class giftAction extends BaseAction{
	
	public function _initialize() {
		parent::_initialize();
	}

	public function _after_gift(){
		echo "do _after_action<br>";
	} 
	
	/**
	 * 礼品中心
	 */
	public function index() {
		$popu_title			= $_REQUEST['popu_title'];	//地点名称
			$gifttype				= $_REQUEST['gifttype'];		//主分类	对应页面左侧主分类
			//$sscode				= $_REQUEST['sscode'];		//子分类	对应页面左侧子分类
			//$ssscode			= $_REQUEST['ssscode'];		//子分类		对应页面右侧主搜索区
			//$brandid			= $_REQUEST['brandid'];		//商户id		对应页面右侧主搜索区
			//$gettype			= $_REQUEST['gettype'];		//商户id		对应页面右侧主搜索区
			//$bprice				= $_REQUEST['bprice'];		//价格上限	对应页面右侧主搜索区
			//$sprice				= $_REQUEST['sprice'];		//价格下限	对应页面右侧主搜索区
			//$stype				= $_REQUEST['stype'];		//排序类型	对应页面右侧主搜索区 'sale' 'new' 'up'  'down'
			$pno				= $_REQUEST['pno'];			//页数
			
			if(empty($gifttype))$gifttype='all';
			if($gifttype=='all')$scode='';
			if($gifttype=='chi')$scode='bakery';
			if($gifttype=='he')$scode='beverage';
			if($gifttype=='wan')$scode='amusement';
			if($gifttype=='other')$scode='giftcard';
			
			
			$stype	= 'new';
			$map['stype'] = $stype;
			if(!in_array($gettype,array('self','delivery')))$gettype	= '';
			if(!empty($gettype))$popu_title	= '';
			if(empty($pno))$pno	= 1;
			if(!empty($scode)||!empty($sscode)||!empty($ssscode)){
					
				/* 查询当前搜索的类别数据 */
				$subsortDict = service('Dict')->findByCode($sscode);
				//echo $sortCode;
				/*查询类别下的所有子类别*/
				$subcode = empty($sscode) ? $scode : $sscode;
				//echo 'subcode='.$subcode;
				if(!empty($subcode)&&$subcode!='goods'){//取得所有子类别，用于查询品牌
						$dicts 	= service('Dict')->getAllDictByCode($subcode);
						$codes = getSubByKey($dicts, 'code');
						$codes[] = $subcode;
						$map['sortcode']=$codes;
						unset($dicts);
				}
				
				if(!empty($subsortDict)&&$sscode!='goods'){
						$dicts 	= service('Dict')->loadDirectSubDicts($subsortDict['id']);
						$scodes = getSubByKey($dicts, 'code');
						$scodes[] = $sscode;
						$map['sortcode']=$scodes;
						$this->assign('subSortList', $dicts);
				}
				if(!empty($ssscode)){unset($map['sortcode']);$ssscodes[]=$ssscode;$map['sortcode']=$ssscodes;}
				
				//商品分类下的品牌
				//$listBrand = service('Brand')->select4UserByGoodsSort($codes);
			}
			//echo "map['sortcode']=";dump($map['sortcode']);
			//$map['bprice']=$bprice;$map['sprice']=$sprice;
			//$map['brandid']=$brandid;$map['stype']=$stype;
			//$map['gettype']=$gettype;
			//$map['popu_title']=$popu_title;
			//热门地点，当前全部查询 ary_brandid
			$populars = model('AreaPopular')->getList($city_id,$area_id);
			
			//添加地点查询条件
			//echo $popu_title;
			if(!empty($popu_title)){
				$bids = service('Brand')->selectBrandIdsByTag( $popu_title ,null, null );
				//dump($bids);
				$map['ary_brandid']=$bids;
				if(!empty($bids)&&count($bids)>0){
					$searchResult 	= service('Product')->search4User($map,20);
					
					//去除点不在地点下的品牌
					/**
					$_listBrand = null;
					foreach($listBrand as $key=>$vo){
						if(in_array($vo['id'],$bids))$_listBrand[]=$listBrand[$key];
					}
					**/
					$listBrand = $_listBrand;
				}else{
					//如果查询的地点存在，但其对应的店铺不存在则返回null
					$searchResult 	= null;
					$listBrand		= null;
				}
				
			}else{
				$searchResult 	= service('Product')->search4User($map,20);
			}
			
			$partners = service('Brand')->selectBSubsByTag( null ,null, null );
			
			$this->assign('partners', $partners);
			$this->assign('searchResult', $searchResult);
			//$this->assign('listBrand', $listBrand);
			$this->assign('populars', $populars);
			//$this->assign('sortCode', $sortCode);
			$this->assign('gifttype', $gifttype);
			//$this->assign('sscode', $sscode);
			//$this->assign('ssscode', $ssscode);
			//$this->assign('brandid', $brandid);
			//$this->assign('bprice', $bprice);
			//$this->assign('sprice', $sprice);
			//$this->assign('stype', $stype);
			//$this->assign('gettype', $gettype);
			$this->assign('popu_title', $popu_title);
			$this->assign('pno', $pno);
			
		$this->display();
	}
	
	
	/**
	 * 商品详情
	 */
	function show(){
		$shelfid	= $_REQUEST['shelfid'];//上架ID。
		if(empty($shelfid))$this->error('查看的商品不存在！');
			
		/*　上架商品数据　*/
		$pservice 		= service('Product');
		$productdata	= $pservice->getByShelfId($shelfid);
		if(empty($productdata))$this->error('查看的商品不存在！');
		$checkresult = $pservice->checkProductStatus($productdata);
		if(	$checkresult['p_status']=='E' || $checkresult['g_status']=='E'
				||	$checkresult['s_status']=='E')$this->error('查看的商品不存在！');
			
			
		/** 同类商品
		$where['goods_sort_id'] = $productdata['goods_sort_id']; 
		$where['shelf_id'] = array('exp',' !='.$shelfid);
		$relativeProduct = $pservice->selectByLimit($where,'',3);
		//dump($relativeProduct);
		 **/	
		/* 页面导航条用到的数据 
		$pcodes = service('Dict')->findAllParents($productdata['goods_sort_id']);
		$tmp = '';
		foreach($pcodes as $vo){
			if($vo['pid']!=0){
				$tmp = $tmp.'s';
				$nav_code[$tmp]=array('id' => $vo['code'],'desc' => $vo['desc']);
			}
		}
		
		$nav_code['p']=array('id' => $productdata['shelf_id'],'desc' => $productdata['goods_title']);
			
		$visitedids = $this->visitedShelf2Cookie($shelfid,9);
		unset($where);
		$where['shelf_id']=array('in',$visitedids);
		$visitedShelfs = service('Product')->selectByLimit($where,'',9);

		$collection = service('Collect')->select4UserIfCollect(array($productdata['shelf_id']),Dict::TOBESEND);
		*/
		$this->assign('productdata', $productdata);
		$this->assign('checkresult', $checkresult);
		//$this->assign('relativeProduct', $relativeProduct);
		//$this->assign('nav_code', $nav_code);
		//$this->assign('visitedShelfs', $visitedShelfs);
		//$this->assign('collection', $collection);
			
		$this->display();
	}
	
	
	function store(){
		$bid = $_REQUEST['bid'];
		$subitem = $_REQUEST['subitem'];
		if(empty($subitem))$subitem = 'goods';
		if(empty($bid))$this->error('品牌不存在！');
		$brand = service('Brand')->getById($bid);
		if(empty($brand))$this->error('品牌不存在！');
		if($subitem=='story'){
			$brandExtInfo = service('Brand')->getExtInfoByBid($bid);
			$this->assign('brandExtInfo', $brandExtInfo);
		}
		$this->assign('brand', $brand);
		$this->assign('subitem', $subitem);
		$this->display();
	}
	
	public function ajxgoods(){
		$brandid			= $_REQUEST['brandid'];	//商户id		对应页面右侧主搜索区
		$stype				= $_REQUEST['stype'];		//排序类型	对应页面右侧主搜索区 'sale' 'new' 'up'  'down'
		$pno				= $_REQUEST['pno'];			//页数
		if(empty($stype))$stype	= 'sale';
		if(empty($pno))$pno	= 1;
		
		$this->assign('stype', $stype);
		$this->assign('brandid', $brandid);
		$this->display('goods_list_box');
	}
	
	public function ajxplace(){
		$this->display('place_list_box');
	}
	
}
